namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorMACD : BaseIndicator
    {
        private BarShape hsMACD;
        private string longCycleEMA = ("MACD.LongCycleEMA." + Common.GetGuid());
        private string shortCycleEMA = ("MACD.ShortCycleEMA" + Common.GetGuid());
        private LineShape tlsDea;
        private LineShape tlsDiff;

        public IndicatorMACD()
        {
            base.IndType = IndicatorType.MACD;
            base.IndName = "MACD";
            base.IndDesc = " 指数平滑异同移动平均线";
            base.IndPeriod.Add(new IndicatorPeriod(0, "SHORT", 12.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "LONG", 26.0));
            base.IndPeriod.Add(new IndicatorPeriod(2, "M", 9.0));
            base.SourceDic["Close"] = "";
            base.fields = new string[] { "MACD", "DIFF", "DEA" };
        }

        public override bool Contains(object obj)
        {
            if (((obj != this.hsMACD) && (obj != this.tlsDea)) && (obj != this.tlsDiff))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double num = 0.0;
            double curValue = 0.0;
            double num3 = 0.0;
            double num4 = 0.0;
            double num5 = 0.0;
            if (r > 0)
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                double num6 = Convert.ToDouble(row[base.SourceDic["Close"]]);
                num4 = Common.CalculateExponentialMovingAvg(this.longCycleEMA, base.SourceDic["Close"], (int) base.IndPeriod[1].ParamValue, num6, base.Crab.DataSource, r);
                num5 = Common.CalculateExponentialMovingAvg(this.shortCycleEMA, base.SourceDic["Close"], (int) base.IndPeriod[0].ParamValue, num6, base.Crab.DataSource, r);
                curValue = num5 - num4;
                Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][this.tlsDea.Name]);
                num3 = Common.CalculateExponentialMovingAvg(this.tlsDea.Name, this.tlsDiff.Name, (int) base.IndPeriod[2].ParamValue, curValue, base.Crab.DataSource, r);
                num = 2.0 * (curValue - num3);
            }
            return new double[] { num, curValue, num3, num4, num5 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.HsMACD, this.TlsDiff, this.TlsDea };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.HsMACD);
            base.Crab.DeleteShape(this.TlsDea);
            base.Crab.DeleteShape(this.TlsDiff);
            base.Crab.DataSource.Columns.Remove(this.LongCycleEMA);
            base.Crab.DataSource.Columns.Remove(this.ShortCycleEMA);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.HsMACD.Name, numArray[0], r);
                        base.Crab.SetValue(this.TlsDiff.Name, numArray[1], r);
                        base.Crab.SetValue(this.TlsDea.Name, numArray[2], r);
                        base.Crab.SetValue(this.LongCycleEMA, numArray[3], r);
                        base.Crab.SetValue(this.ShortCycleEMA, numArray[4], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
            base.IndPeriod[2].ParamValue = periods[2];
            Common.GetGuid();
            this.HsMACD = base.Crab.AddBar(fields[0], panelID);
            base.Crab.SetBar(fields[0], fields[0], Color.Red, Color.SkyBlue, 1, 0.01, 2, BarStyle.Line);
            this.TlsDiff = base.Crab.AddLine(fields[1], panelID);
            base.Crab.SetLine(fields[1], fields[1], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsDea = base.Crab.AddLine(fields[2], panelID);
            base.Crab.SetLine(fields[2], fields[2], Color.Yellow, 1, 0.01, 2, DashStyle.Solid);
            base.SourceDic["Close"] = source[0];
            base.Crab.DataSource.Columns.Add(this.LongCycleEMA);
            base.Crab.DataSource.Columns.Add(this.ShortCycleEMA);
        }

        public BarShape HsMACD
        {
            get
            {
                return this.hsMACD;
            }
            set
            {
                this.hsMACD = value;
            }
        }

        public string LongCycleEMA
        {
            get
            {
                return this.longCycleEMA;
            }
            set
            {
                this.longCycleEMA = value;
            }
        }

        public string ShortCycleEMA
        {
            get
            {
                return this.shortCycleEMA;
            }
            set
            {
                this.shortCycleEMA = value;
            }
        }

        public LineShape TlsDea
        {
            get
            {
                return this.tlsDea;
            }
            set
            {
                this.tlsDea = value;
            }
        }

        public LineShape TlsDiff
        {
            get
            {
                return this.tlsDiff;
            }
            set
            {
                this.tlsDiff = value;
            }
        }
    }
}

